;
var helper = require("../../javascript/helper");
var request = require("request");
var db = require("../../javascript/mysql");

var mysql = new db.mysqlClient("accident");
var spider = function () {
    var that = this;
    //生成产品数据
    this.genProductInfo = function () {
        let days = [];
        let projects = [];
        for (i = 1; i < 91; i++) {
            days.push(i.toString() + "d")
        }
        days.push("365d");
        plan = {
            planA: {
                "year": ["0-17"],
                "days": days,
                "people_count": ["1"],
                "accident_death": ["100000"],
                "common_traffic": [""],
                "accident_medical": ["100000"],
                "exigency_back": ["100000"],
                "death_back": ["200000"],
                "casket_fee": ["8000"],
                "burial_fee": ["16000"],
                "child_return": ["8000"],
                "relate_mourn": ["8000"],
                "nervous_help": ["40000"]
            },
            planB: {
                "year": ["18-80"],
                "days": days,
                "people_count": ["1"],
                "accident_death": ["100000"],
                "common_traffic": ["100000"],
                "accident_medical": ["100000"],
                "exigency_back": ["100000"],
                "death_back": ["200000"],
                "casket_fee": ["8000"],
                "burial_fee": ["16000"],
                "child_return": ["8000"],
                "relate_mourn": ["8000"],
                "nervous_help": ["40000"]
            }
        }
        for (k in plan) {
            data = helper.product([
                plan[k]["year"], plan[k]["days"], plan[k]["people_count"], plan[k]["accident_death"], plan[k]["common_traffic"], plan[k]["accident_medical"], plan[k]["exigency_back"], plan[k]["death_back"], plan[k]["casket_fee"], plan[k]["burial_fee"], plan[k]["child_return"], plan[k]["relate_mourn"], plan[k]["nervous_help"]
            ])
            for (i = 0; i < data.length; i++) {
                dt = helper.formatProductInfo(data[i], plan[k]);
                projects.push(dt);
            }
        }
        return projects;

    }
    //请求数据
    this.httpRequest = function (dt) {
        if (dt.year == "0-17") {
            planCd = "nets0219"
        } else {
            planCd = "nets0218"
        }
        params = {
            "productNo": "21",
            "insuredSex": "",
            "insuredBirthday": "",
            "insuranceYears": dt.days.replace("d",""),
            "paymentPeriod": "",
            "guaranteeAmount": "",
            "mult": "0",
            "payIntv": "0",
            "insuyearFlag": "D",
            "payendyearFlag": "",
            "contPlanCode": planCd
        }
        return new Promise(function (resolve, reject) {
            request({
                url: "https://www.ergo-life.cn//Admin/getCALCUData.do",
                method: "POST",
                json: false,
                rejectUnauthorized: false,
                headers: {
                    "Accept": "application/json, text/javascript, */*; q=0.01",
                    "Accept-Encoding": "gzip, deflate, br",
                    "Accept-Language": "zh-CN,zh;q=0.9",
                    "Connection": "keep-alive",
                    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
                    "Cookie": "_ga=GA1.2.1753139979.1576025815; _gid=GA1.2.19320675.1590457174; JSESSIONID=6DC4F339D73B7AF7F896B4D42CDC8BB1",
                    "Sec-Fetch-Dest": "empty",
                    "Sec-Fetch-Mode": "cors",
                    "Sec-Fetch-Site": "same-origin",
                    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36",
                    "X-Requested-With": "XMLHttpRequest"
                },
                body: helper.urlencode(params)
            }, function (err, res, body) {
                if (!err && res.statusCode == 200) {
                    if (body.indexOf("result") != -1) {
                        rs = {
                            name: "境外亚洲游保险",
                            company: "德华安顾人寿",
                            price: Number(JSON.parse(body).result).toFixed(2).toString()
                        }
                        resolve( rs );
                    }
                }
            })

        })
    }
    this.main = async function(){
        let filters = [];
        data = that.genProductInfo();
        for( i = 0 ;i < data.length;i++){
            dt = data[i];
            let rs =  await that.httpRequest( dt );
            let price = rs.price;
            if (!filters.includes( price  )){
                filters.push(price)
                result = Object.assign( dt, rs );
                mysql.insertData( result );
                
            }
           
        }
        mysql.close();
    
    }

}